package mo.org.cm309.shoppingcart.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mo.org.cm309.shoppingcart.dao.UserDAO;
import mo.org.cm309.shoppingcart.entity.User;
import mo.org.cm309.shoppingcart.service.UserService;

/**
 * Servlet implementation class UserProfileEditServlet
 */
@WebServlet("/UserProfileEditServlet")
public class UserProfileEditServlet extends HttpServlet {
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		UserService uservice = new UserService(); 
		User signeduser = uservice.getUserById(4);
		req.setAttribute("signedUser", signeduser);
		req.getRequestDispatcher("/userProfileEdit.jsp").forward(req, resp);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		User updateduser = new User();
		
		String id = req.getParameter("id");
		String firstName   = req.getParameter("firstName");
		String lastName    = req.getParameter("lastName");
		String eMail       = req.getParameter("eMail");
		String password    = req.getParameter("password");
		String rePassword  = req.getParameter("rePassword");
		String phoneNumber = req.getParameter("phoneNumber");
		String address     = req.getParameter("address");
		String creditCardNo= req.getParameter("creditCardNo");
		String cardExpiry  = req.getParameter("cardExpiry");
		
		updateduser.setId(Long.parseLong(id));
		updateduser.setFirstname(firstName);
		updateduser.setLastname(lastName);
		updateduser.setEmail(eMail);
		updateduser.setPassword(password);
		updateduser.setPhoneNum(phoneNumber);
		updateduser.setAddress(address);
		updateduser.setCreditCard(creditCardNo);
		updateduser.setCardExpiry(cardExpiry);
//		updateduser.setRoleId(RoleService.getrService().getIdByName(CommonUtility.ROLE_ADMIN_NAME));
		UserService uservice = new UserService(); 
		uservice.updateProfile(updateduser);		
	}
}
